package com.tcoiss.datafactory.controller;

import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.datafactory.domain.KTrans;
import com.tcoiss.datafactory.service.IKTransService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 转换Controller
 * 
 * @author zw
 * @date 2021-06-01
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/trans" )
public class KTransController extends BaseController {

    private final IKTransService iKTransService;

    /**
     * 查询转换列表
     */
    @PreAuthorize(hasPermi = "datafactory:trans:list")
    @GetMapping("/list")
    public TableDataInfo list(KTrans kTrans) {
        startPage();
        List<KTrans> list = iKTransService.queryList(kTrans);
        return getDataTable(list);
    }
    /**
     * 查询转换列表
     */
    @PreAuthorize(hasPermi = "datafactory:trans:list")
    @GetMapping("/getTrans")
    public List<KTrans> getTransList(KTrans kTrans) {
        return iKTransService.queryList(kTrans);
    }
    /**
     * 导出转换列表
     */
    @PreAuthorize(hasPermi = "datafactory:trans:export" )
    @Log(title = "转换" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,KTrans kTrans) throws IOException
    {
        List<KTrans> list = iKTransService.queryList(kTrans);
        ExcelUtil<KTrans> util = new ExcelUtil<KTrans>(KTrans.class);
        util.exportExcel(response,list, "trans" );
    }

    /**
     * 获取转换详细信息
     */
    @PreAuthorize(hasPermi = "datafactory:trans:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iKTransService.getById(id));
    }

    /**
     * 启动ETL转换
     */
    @GetMapping(value = "/startTrans.do/{id}" )
    public AjaxResult startTrans(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iKTransService.startTransById(id));
    }

    /**
     * 新增转换
     */
    @PreAuthorize(hasPermi = "datafactory:trans:add" )
    @Log(title = "转换" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KTrans kTrans) {
        return toAjax(iKTransService.save(kTrans) ? 1 : 0);
    }

    /**
     * 修改转换
     */
    @PreAuthorize(hasPermi = "datafactory:trans:edit" )
    @Log(title = "转换" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KTrans kTrans) {
        return toAjax(iKTransService.updateById(kTrans) ? 1 : 0);
    }

    /**
     * 删除转换
     */
    @PreAuthorize(hasPermi = "datafactory:trans:remove" )
    @Log(title = "转换" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}" )
    public AjaxResult remove(@PathVariable Long[] id) {
        return toAjax(iKTransService.removeByIds(Arrays.asList(id)) ? 1 : 0);
    }
}
